package common.liusany.admin.web.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import common.liusany.admin.entity.account.Group;
import common.liusany.admin.entity.account.Permission;
import common.liusany.admin.service.account.AccountManager;

@Controller
@RequestMapping(value = "/admin/group")
public class GroupDetailController extends BaseCopntroller{

	private AccountManager accountManager;

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(Model model) {
		model.addAttribute("allPermissions", Permission.values());
		return "admin/groupForm";
	}

	@RequestMapping(value = "save/{id}", method = RequestMethod.POST)
	public String save(@ModelAttribute("group") Group group,
			RedirectAttributes redirectAttributes,Model model) {
		accountManager.saveGroup(group);
		redirectAttributes.addFlashAttribute("message",
				"修改权限组" + group.getName() + "成功");
		redirectUrl = "admin/group/list";
		model.addAttribute("message", "修改权限组" + group.getName() + "成功");
		model.addAttribute("redirectUrl", redirectUrl);
		
		return SUCCESS;
	}

	@ModelAttribute("group")
	public Group getGroup(@PathVariable("id") Long id) {
		return accountManager.getGroup(id);
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

}
